<?php

namespace addons\company\controller\api;

use app\admin\model\company\Example;
use app\admin\model\company\ExampleIndustry;
use app\admin\model\company\ExampleType;
use app\admin\model\company\Product;
use app\admin\model\company\Setting;
use app\admin\model\company\Share;
use app\admin\model\company\ShareCollection;
use app\admin\model\company\ShareType;
use app\admin\model\company\Testimonial;
use app\common\controller\Api;

class Page extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function indexPage()
    {
        //推荐产品
        $productIds = Setting::getValueByKey('homepage_recommend_product');
        $products = Product::all($productIds);
        $products = array_map(function ($e) {
            return [
                'id' => $e->id,
                'name' => $e->name,
                'coverimage' => cdnurl($e->coverimage),
                'price' => $e->price,
                'updatedate' => $e->updatedate,
                'summary' => $e->summary,
            ];
        }, $products);

        //运营开发分享
//        $shareIds = Setting::getValueByKey('homepage_share');
//        $shares = Share::all($shareIds, ['sharetype', 'sharecollection']);
        $shareModel = new Share();
        $shares = $shareModel->with(['sharetype', 'sharecollection'])->order('id', 'desc')->limit(6)->select();
        $shares = array_map(function ($e) {
            return [
                'id' => $e->id,
                'name' => $e->name,
                'coverimage' => cdnurl($e->coverimage),
                'updatedate' => $e->updatetime,
                'sharetype' => $e->sharetype,
            ];
        }, $shares);


        //设计案例
        $exampleIds = Setting::getValueByKey('homepage_mobile_example');
        $examples = Example::all($exampleIds, ['exampletype', 'exampleindustry']);
        $examples = array_map(function ($e) {
            return [
                'id' => $e->id,
                'name' => $e->name,
                'coverimage' => cdnurl($e->coverimage),
                'type' => $e->exampletype,
                'industry' => $e->exampleindustry,
            ];
        }, $examples);

        // 更多案例
        $softTypes = ExampleType::all();
        $industryTypes = ExampleIndustry::all();
        $allExamples = Example::all();
        foreach ($softTypes as $k=>$v) {
            $softTypes[$k]['item_count'] = count(array_filter($allExamples, function ($e) use ($v) {
                if ($e->example_type_id == $v['id']) {
                    return true;
                }
                return false;
            }));
        }
        $softTypes = array_map(function ($e) {
            return [
                'id' => $e['id'],
                'name' => $e['type_name'],
                'count' => $e['item_count'],
                't' => 'softtype'
            ];
        }, $softTypes);

        foreach ($industryTypes as $k=>$v) {
            $industryTypes[$k]['item_count'] = count(array_filter($allExamples, function ($e) use ($v) {
                if ($e->example_industry_id == $v['id']) {
                    return true;
                }
                return false;
            }));
        }
        $industryTypes = array_map(function ($e) {
            return [
                'id' => $e['id'],
                'name' => $e['industry_name'],
                'count' => $e['item_count'],
                't' => 'industrytype'
            ];
        }, $industryTypes);

        //客户怎么说
        $testimonials = Testimonial::all();
        $testimonials = array_map(function ($e) {
            return [
                'id' => $e->id,
                'name' => $e->name,
                'position' => $e->position,
                'company_name' => $e->company_name,
                'avatarimage' => cdnurl($e->avatarimage),
                'comment' => $e->comment,
            ];
        }, $testimonials);


        $this->success('ok', [
            'recommend_products' => $products,
            'shares' => $shares,
            'examples' => $examples,
            'soft_types' => $softTypes,
            'industry_types' => $industryTypes,
            'testimonials' => $testimonials,
            'contact_phone' => str_replace('-', '', Setting::getValueByKey('contact_telephone'))
        ]);
    }

    public function custonwebsitepage()
    {
        // 视频
        $shareIds = Setting::getValueByKey('website_share');
        $shareIds = explode(',', $shareIds);
        $shareIds = array_slice($shareIds, 0, 3);
        $shares = Share::all($shareIds, ['sharetype', 'sharecollection']);

        $shares = array_map(function ($e) {
            return [
                'id' => $e->id,
                'name' => $e->name,
                'coverimage' => cdnurl($e->coverimage),
                'summary' => $e->summary,
                'updatedate' => $e->updatetime,
                'sharetype' => $e->sharetype,
                'sharecollection' => $e->sharecollection,
            ];
        }, $shares);

        //网站设计案例
        $exampleIds = Setting::getValueByKey('website_example');
        $exampleIds = explode(',', $exampleIds);
        $exampleIds = array_slice($exampleIds, 0, 5);
        $examples = Example::all($exampleIds, ['exampletype', 'exampleindustry']);
        $examples = array_map(function ($e) {
            return [
                'id' => $e->id,
                'name' => $e->name,
                'coverimage' => cdnurl($e->coverimage),
                'type' => $e->exampletype,
                'industry' => $e->exampleindustry,
            ];
        }, $examples);

        //更多案例
        $industryTypes = ExampleIndustry::all();
        $allExamples = Example::all();

        foreach ($industryTypes as $k=>$v) {
            $industryTypes[$k]['item_count'] = count(array_filter($allExamples, function ($e) use ($v) {
                if ($e->example_industry_id == $v['id'] and 1 == $e->example_type_id) {
                    return true;
                }
                return false;
            }));
        }
        $industryTypes = array_map(function ($e) {
            return [
                'id' => $e['id'],
                'name' => $e['industry_name'],
                'count' => $e['item_count'],
                't' => 'industrytype'
            ];
        }, $industryTypes);

        $industryTypes = array_slice(array_filter($industryTypes, function ($e) {
            if ($e['count'] > 0) {
                return true;
            }
            return false;
        }), 0, 8);

        $this->success('ok', [
            'shares' => $shares,
            'examples' => $examples,
            'industry_types' => $industryTypes,
        ]);
    }

    public function custonminiapp()
    {
        // 视频
        $shareIds = Setting::getValueByKey('miniprogram_share');
        $shareIds = explode(',', $shareIds);
        $shareIds = array_slice($shareIds, 0, 3);
        $shares = Share::all($shareIds, ['sharetype', 'sharecollection']);

        $shares = array_map(function ($e) {
            return [
                'id' => $e->id,
                'name' => $e->name,
                'coverimage' => cdnurl($e->coverimage),
                'summary' => $e->summary,
                'updatedate' => $e->updatetime,
                'sharetype' => $e->sharetype,
                'sharecollection' => $e->sharecollection,
            ];
        }, $shares);

        //网站设计案例
        $exampleIds = Setting::getValueByKey('miniprogram_example');
        $exampleIds = explode(',', $exampleIds);
        $exampleIds = array_slice($exampleIds, 0, 5);
        $examples = Example::all($exampleIds, ['exampletype', 'exampleindustry']);
        $examples = array_map(function ($e) {
            return [
                'id' => $e->id,
                'name' => $e->name,
                'coverimage' => cdnurl($e->coverimage),
                'type' => $e->exampletype,
                'industry' => $e->exampleindustry,
            ];
        }, $examples);

        //更多案例
        $industryTypes = ExampleIndustry::all();
        $allExamples = Example::all();

        foreach ($industryTypes as $k=>$v) {
            $industryTypes[$k]['item_count'] = count(array_filter($allExamples, function ($e) use ($v) {
                if ($e->example_industry_id == $v['id'] and 2 == $e->example_type_id) {
                    return true;
                }
                return false;
            }));
        }
        $industryTypes = array_map(function ($e) {
            return [
                'id' => $e['id'],
                'name' => $e['industry_name'],
                'count' => $e['item_count'],
                't' => 'industrytype'
            ];
        }, $industryTypes);

        $industryTypes = array_slice(array_filter($industryTypes, function ($e) {
            if ($e['count'] > 0) {
                return true;
            }
            return false;
        }), 0, 8);

        $this->success('ok', [
            'shares' => $shares,
            'examples' => $examples,
            'industry_types' => $industryTypes,
        ]);
    }

    public function productlistpage()
    {
        $products = Product::where(function ($query) {
            return $query->where('status', 1);
        })->order('weigh', 'desc')->select();
        $products = array_map(function ($e) {
            return [
                'id' => $e->id,
                'name' => $e->name,
                'coverimage' => cdnurl($e->coverimage),
                'price' => $e->price,
                'updatedate' => $e->updatedate,
                'summary' => $e->summary,
            ];
        }, $products);
        $this->success('ok', $products);
    }

    public function productDetail()
    {
        $id = $this->request->get('id');
        $solution = Product::get($id);

        $solution = array_merge($solution->toArray(), [
            'coverimage' => cdnurl($solution->coverimage, true),
            'cover5v4image' => cdnurl($solution->cover5v4image, true),
            'cover1v1image' => cdnurl($solution->cover1v1image, true),
        ]);

        $this->success('ok', $solution);
    }

    public function examplelistpage()
    {
        $page = input('page', 1);
        $limit = 10;

        $model = new Example();
        if ($exampleTypeId = input('example_type_id', 0)) {
            $model->where('example_type_id', $exampleTypeId);
        }
        if ($exampleIndustryId = input('example_industry_id', 0)) {
            $model->where('example_industry_id', $exampleIndustryId);
        }

        $list = $model->with(['exampletype', 'exampleindustry'])->order('id', 'desc')->paginate($limit, false, ['page' => $page]);
        $list = $list->toArray();
        $data = $list['data'];
        $data = array_map(function ($e) {
            return array_merge($e, [
                'coverimage' => cdnurl($e['coverimage'], true),
                'cover5v4image' => cdnurl($e['cover5v4image'], true),
                'cover1v1image' => cdnurl($e['cover1v1image'], true),
            ]);
        }, $data);
        $list['data'] = $data;
        $list['exampletypes'] = ExampleType::all();
        $list['exampleindustries'] = ExampleIndustry::all();
        if (count($list)) {
            $this->success('ok', $list);
        } else {
            $this->error('没有更多数据');
        }
    }

    public function exampledetail()
    {
        $id = $this->request->get('id');
        $example = Example::get($id);

        $example = array_merge($example->toArray(), [
            'coverimage' => cdnurl($example->coverimage, true),
            'cover5v4image' => cdnurl($example->cover5v4image, true),
            'cover1v1image' => cdnurl($example->cover1v1image, true),
        ]);

        $this->success('ok', $example);
    }

    public function sharelistpage()
    {
        $page = input('page', 1);
        $limit = 10;

        $model = new Share();
        if ($shareTypeId = input('share_type_id', 0)) {
            $model->where('share_type_id', $shareTypeId);
        }
        if ($shareCollectionId = input('share_collection_id', 0)) {
            $model->where('share_collection_id', $shareCollectionId);
        }

        $list = $model->with(['sharetype', 'sharecollection'])->order('id', 'desc')->paginate($limit, false, ['page' => $page]);
        $list = $list->toArray();
        $data = $list['data'];
        $data = array_map(function ($e) {
            return array_merge($e, [
                'coverimage' => cdnurl($e['coverimage'], true),
                'cover5v4image' => cdnurl($e['cover5v4image'], true),
                'cover1v1image' => cdnurl($e['cover1v1image'], true),
            ]);
        }, $data);
        $list['data'] = $data;
        $list['sharetypes'] = ShareType::all();
        $list['sharecollections'] = ShareCollection::all();
        if (count($list)) {
            $this->success('ok', $list);
        } else {
            $this->error('没有更多数据');
        }
    }

    public function shareDetail()
    {
        $id = $this->request->get('id');
        $share = Share::get($id, ['sharetype', 'sharecollection']);

        if (is_null($share)) {
            $this->error('没有这个数据');
        }

        $share = array_merge($share->toArray(), [
            'coverimage' => cdnurl($share->coverimage, true),
            'cover5v4image' => cdnurl($share->cover5v4image, true),
            'cover1v1image' => cdnurl($share->cover1v1image, true),
        ]);

        $this->success('ok', $share);

    }

    public function contactus()
    {
        $settingData = Setting::all();
        $settingKeyValue = [];
        foreach ($settingData as $v) {
            $settingKeyValue[$v->name] = $v->value;
        }
        $this->success('ok', [
            'contact_telephone' => $settingKeyValue['contact_telephone'],
            'work_time' => $settingKeyValue['work_time'],
            'address' => $settingKeyValue['address'],
            'email' => $settingKeyValue['email'],
            'weixin_image' => cdnurl($settingKeyValue['weixin_image'], true),
            'weixin' => $settingKeyValue['weixin'],
            'gzh_image' => cdnurl($settingKeyValue['gzh_image'], true),
            'gzh' => $settingKeyValue['gzh'],
            'map_image' => cdnurl($settingKeyValue['map_image'], true),
        ]);
    }
}
